﻿using System;

using Gonte.Reflection;

namespace Gonte.Wpf.Controls.ViewModel
{
    public class ItemObjectViewModel
        : ItemViewModel
    {
        #region Constructors

        public ItemObjectViewModel(ItemObjectViewModelInfo viewModelInfo, IViewModel parent, object model)
            : base(viewModelInfo, parent, false)
        {
            PopulateViewModelFromModel(model);
        } 

        #endregion

        #region Properties

        /// <summary>
        /// The model this view model maps to
        /// </summary>
        public new object Model
        {
            get { return _model; }

            set
            {
                PopulateViewModelFromModel(value);
            }
        } 

        #endregion

        #region Helpers

        private void PopulateViewModelFromModel(object model)
        {
            //if (model == null)
            //{
            //    return;
            //}

            _model = model;
            Type type = TypeHelper.UnwrapType(_model.GetType());
            TypeAccessor typeAccessor = TypeAccessor.GetTypeAccessor(type);
            ViewModelInfo viewModelInfo = ViewModelInfo.GetInfo(type);

            // Set the label
            _label = (string)typeAccessor.GetValue(_model, viewModelInfo.LabelProperty);

            // Set the icon
            UpdateIconSource();
        }  

        #endregion
    }
}
